package com.katze.boot.zookeeper.controller;

import com.katze.boot.plugins.feign.FeignRequester;
import com.katze.boot.plugins.shiro.annotations.RestRequestMapping;
import com.katze.boot.zookeeper.entity.ZookeeperEntity;
import com.katze.boot.zookeeper.service.FeignZookeeperService;
import com.katze.boot.zookeeper.service.ZookeeperService;
import com.katze.common.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/zookeeper")
public class ZookeeperController extends FeignRequester<FeignZookeeperService> {

    @Autowired
    private ZookeeperService zookeeperService;

    @GetMapping(value = "/find")
    public Message find(Long owner) {
        return Message.ok().data(zookeeperService.findByOwner(owner));
    }

    /**
     * 创建连接信息
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message create (ZookeeperEntity entity) {
        ZookeeperEntity exist = zookeeperService.exists(entity);
        if (exist != null) {
            return Message.error(entity.getServer() + "连接已存在(对应连接名称:[" + exist.getTitle() + "])");
        }
        entity.setCreateTime(new Date());
        if (zookeeperService.save(entity)) {
            return Message.ok(entity.getTitle() + "连接创建成功");
        } else {
            return Message.error(entity.getTitle() + "连接创建失败");
        }
    }

    /**
     * 更新连接信息
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public Message update (ZookeeperEntity entity) {
        ZookeeperEntity exist = zookeeperService.exists(entity);
        if (exist != null) {
            return Message.error("更新失败," + entity.getServer() + "连接已存在(对应连接名称:[" + exist.getTitle() + "])");
        }
        if (zookeeperService.updateById(entity)) {
            return Message.ok(entity.getTitle() + "连接更新成功");
        } else {
            return Message.error(entity.getTitle() + "连接更新失败");
        }
    }

    /**
     * 删除连接信息
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Message delete (String id) {
        ZookeeperEntity entity = zookeeperService.getById(id);
        if (entity != null) {
            if (zookeeperService.removeById(entity.getId())){
                return Message.ok(entity.getTitle() + "连接删除成功");
            } else {
                return Message.error(entity.getTitle() + "连接删除失败");
            }
        } else {
            return Message.error("连接已不存在");
        }
    }

    @GetMapping(value = "/exists/{uid}")
    public Message exists (@PathVariable("uid") String uid, String id) {
        return  execute(uid, (uri, service) -> service.exists(uri, id));
    }

    @RestRequestMapping(value = "/{method}/{uid}",
            method = RequestMethod.POST,
            desc = "connection:连接zk组件; createNode:创建节点; children:获取子节点; " +
                    "detail:节点详信息; refresh:刷新节点; deleteNode:删除节点;")
    public Message post (@PathVariable("uid") String uid, @PathVariable("method") String method, ZookeeperEntity entity) {
        return execute(uid, (uri, service) -> service.post(uri, method, entity));
    }

    @RestRequestMapping(value = "/{method}/{uid}",
            method = RequestMethod.PUT,
            desc = "saveData:保存;")
    public Message put (@PathVariable("uid") String uid, @PathVariable("method") String method, ZookeeperEntity entity) {
        return execute(uid, (uri, service) -> service.put(uri, method, entity));
    }

    @RestRequestMapping(value = "/{method}/{uid}", method = RequestMethod.DELETE, desc = "close:关闭连接;")
    public Message delete (@PathVariable("uid") String uid, @PathVariable("method") String method, String id) {
        return execute(uid, (uri, service) -> service.delete(uri, method, id));
    }
}
